﻿using Common.Class;
using Common.Model;
using SqlSugar;

namespace BLL
{
    public class RoleBLL
    {
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        public static Dictionary<long, RoleModel> RoleListGet()
        {

            Dictionary<long, RoleModel> _ml = null;
            string _connnectStr = BLL.ConfigBLL.DBConfigConnectString;
            SqlSugarClient _db = SugarHelper.GetSqlSugarClient(BLL.ConfigBLL.DBType, _connnectStr, CommonBLL.DBRunTimeOut, CommonBLL.IsDBLog, false);
            _ml = _db.Queryable<RoleModel>().Where(m => m.DeleteFlag == "N").ToList().ToDictionary(m => m.ID.Value);
            return _ml;
        }

        /// <summary>
        /// 根据ID数组获取列表
        /// </summary>
        /// <param name="IDs"></param>
        /// <returns></returns>
        public static Dictionary<long, RoleModel> RoleListGetFromID(long[] IDs)
        {
            Dictionary<long, RoleModel> _ml = null;
            string _connnectStr = BLL.ConfigBLL.DBConfigConnectString;
            SqlSugarClient _db = SugarHelper.GetSqlSugarClient(BLL.ConfigBLL.DBType, _connnectStr, CommonBLL.DBRunTimeOut, CommonBLL.IsDBLog, false);
            _ml = _db.Queryable<RoleModel>().Where(m => IDs.Contains(m.ID.Value)).ToList().ToDictionary(m => m.ID.Value);
            return _ml;
        }

        /// <summary>
        /// 根据ID获取数据
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static RoleModel RoleGetFromID(long ID)
        {
            Dictionary<long, RoleModel> _ml = RoleListGetFromID(new long[] { ID });
            if (_ml != null && _ml.Count > 0)
            {
                return _ml.Values.ToList()[0];
            }
            return null;
        }

        /// <summary>
        /// 增加操作员
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static RoleModel RoleAdd(RoleModel model)
        {
            string _connnectStr = BLL.ConfigBLL.DBConfigConnectString;
            SqlSugarClient _db = SugarHelper.GetSqlSugarClient(BLL.ConfigBLL.DBType, _connnectStr, CommonBLL.DBRunTimeOut, CommonBLL.IsDBLog, false);
            model.CreateTime = DateTime.Now;
            RoleModel _m = _db.Insertable<RoleModel>(model).ExecuteReturnEntity();
            return _m;
        }

        /// <summary>
        /// 更新操作员
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool RoleUpdate(RoleModel model)
        {
            string _connnectStr = BLL.ConfigBLL.DBConfigConnectString;
            SqlSugarClient _db = SugarHelper.GetSqlSugarClient(BLL.ConfigBLL.DBType, _connnectStr, CommonBLL.DBRunTimeOut, CommonBLL.IsDBLog, false);

            if (_db.Updateable<RoleModel>(model).UpdateColumns(model.ChangeProperty.ToArray(), true).ExecuteCommand() > 0)
            {
                model.ChangeProperty = null;
                return true;
            }
            else
            {
                return false;
            } 
        }

        /// <summary>
        /// 获取角色关联的会话
        /// </summary>
        /// <param name="roleID">角色ID</param>
        /// <returns></returns>
        public static List<long> RoleSessionListGet(long roleID)
        {

            List<long> _ml = null;
            string _connnectStr = BLL.ConfigBLL.DBConfigConnectString;
            SqlSugarClient _db = SugarHelper.GetSqlSugarClient(BLL.ConfigBLL.DBType, _connnectStr, CommonBLL.DBRunTimeOut, CommonBLL.IsDBLog, false);
            _ml = _db.Queryable<RoleSessionModel>().Where(m => m.RoleID == roleID && m.DeleteFlag == "N").Select(m => m.SessionID.Value).ToList();
            return _ml;
        }

        /// <summary>
        /// 角色与会话关联
        /// </summary>
        /// <param name="roleID">角色ID</param>
        /// <param name="sessionIDs">会话ID列表</param>
        /// <returns></returns>
        public static List<long> RoleSessionListAdd(long roleID, List<long> sessionIDs)
        {

            List<long> _ml = null;
            string _connnectStr = BLL.ConfigBLL.DBConfigConnectString;
            SqlSugarClient _db = SugarHelper.GetSqlSugarClient(BLL.ConfigBLL.DBType, _connnectStr, CommonBLL.DBRunTimeOut, CommonBLL.IsDBLog, false);
            try
            {
                _db.BeginTran();
                //废弃历史关联
                _db.Updateable<RoleSessionModel>().SetColumns(it => new RoleSessionModel() { DeleteFlag = "Y" }).Where(m => m.RoleID == roleID && m.DeleteFlag == "N").ExecuteCommand();
                //新增关联
                foreach (long session in sessionIDs)
                {
                    RoleSessionModel _roleSession = new RoleSessionModel();
                    _roleSession.ID = CommonHelper.GetNewID();
                    _roleSession.RoleID = roleID;
                    _roleSession.SessionID = session;
                    _roleSession.CreateTime = DateTime.Now;
                    _roleSession.DeleteFlag = "N";
                    _db.Insertable<RoleSessionModel>(_roleSession).ExecuteReturnEntity();
                }

                _db.CommitTran();
            }
            catch (Exception ex)
            {
                _db.RollbackTran();
                throw ex;
            }


            return _ml;
        }

    }
}
